package mathsys;

import java.io.*;
import java.util.*;

public class MathExerciseGeneratorV2 {
    // 生成指定范围内的随机整数
    private static int getRandomNumber(int min, int max) {
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }

    // 生成带答案的加减法算式
    private static Map<String, Integer> generateExerciseWithAnswer() {
        Map<String, Integer> exercise = new HashMap<>();
        int num1 = getRandomNumber(0, 100);
        int num2 = getRandomNumber(0, 100);
        int operator = getRandomNumber(0, 1); // 0:加法, 1:减法
        String expression;
        int result;

        if (operator == 0) { // 加法
            // 确保和不超过100
            if (num1 + num2 > 100) {
                return generateExerciseWithAnswer();
            }
            expression = num1 + " + " + num2 + " = ?";
            result = num1 + num2;
        } else { // 减法
            // 确保差不小于0
            if (num1 < num2) {
                int temp = num1;
                num1 = num2;
                num2 = temp;
            }
            expression = num1 + " - " + num2 + " = ?";
            result = num1 - num2;
        }

        exercise.put(expression, result);
        return exercise;
    }

    // 生成HTML文件
    private static void generateHTML(List<Map<String, Integer>> exercises) {
        try (PrintWriter out = new PrintWriter(new FileWriter("math_exercises_v0.2.html"))) {
            out.println("<!DOCTYPE html>");
            out.println("<html lang=\"zh-CN\">");
            out.println("<head>");
            out.println("    <meta charset=\"UTF-8\">");
            out.println("    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
            out.println("    <title>加减法口算练习系统-v0.2</title>");
            out.println("    <style>");
            out.println("        body { font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }");
            out.println("        h1 { color: #333; text-align: center; }");
            out.println("        .exercise-container { margin: 30px auto; display: grid;");
            out.println("            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));");
            out.println("            gap: 15px 20px; }");
            out.println("        .exercise { font-size: 16px; padding: 12px; border: 1px solid #ddd;");
            out.println("            border-radius: 5px; background-color: #f9f9f9; }");
            out.println("        .answer { color: #666; font-size: 14px; margin-top: 5px; display: none; }");
            out.println("        .show-answers { text-align: center; margin: 20px; }");
            out.println("        button { padding: 8px 16px; font-size: 16px; cursor: pointer;");
            out.println("            background-color: #4CAF50; color: white; border: none; border-radius: 4px; }");
            out.println("        button:hover { background-color: #45a049; }");
            out.println("    </style>");
            out.println("    <script>");
            out.println("        function toggleAnswers() {");
            out.println("            const answers = document.getElementsByClassName('answer');");
            out.println("            for (let ans of answers) {");
            out.println("                ans.style.display = ans.style.display === 'block' ? 'none' : 'block';");
            out.println("            }");
            out.println("        }");
            out.println("    </script>");
            out.println("</head>");
            out.println("<body>");
            out.println("    <h1>加减法口算练习系统-v0.2</h1>");
            out.println("    <div class=\"show-answers\">");
            out.println("        <button onclick=\"toggleAnswers()\">显示/隐藏答案</button>");
            out.println("    </div>");
            out.println("    <div class=\"exercise-container\">");

            // 循环生成习题
            int count = 1;
            for (Map<String, Integer> exercise : exercises) {
                String expression = exercise.keySet().iterator().next();
                int answer = exercise.values().iterator().next();

                out.println("        <div class=\"exercise\">");
                out.println("            <span class=\"number\">" + count + ". </span>");
                out.println("            <span class=\"expression\">" + expression + "</span>");
                out.println("            <div class=\"answer\">答案: " + answer + "</div>");
                out.println("        </div>");
                count++;
            }

            out.println("    </div>");
            out.println("</body>");
            out.println("</html>");

            System.out.println("习题已生成，请打开 math_exercises_v0.2.html 文件查看");
        } catch (IOException e) {
            System.err.println("生成HTML文件时出错: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        List<Map<String, Integer>> exercises = new ArrayList<>();

        // 生成50道带答案的习题
        for (int i = 0; i < 50; i++) {
            exercises.add(generateExerciseWithAnswer());
        }

        // 生成HTML页面
        generateHTML(exercises);
    }
}